package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.PostCmt;
import cn.zucc.edu.handyzucc.entity.ShopCmt;
import cn.zucc.edu.handyzucc.service.CommentService;
import cn.zucc.edu.handyzucc.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "getPostCommentByPost", method = RequestMethod.POST)
    private Map<String, Object> getPostCommentByPost(Integer postcmt_post) {
        Map<String, Object> modelMap = new HashMap<>();
        List<PostCmt> postCmts = commentService.getPostCommentsByPost(postcmt_post);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", postCmts);
        return modelMap;
    }

    @RequestMapping(value = "getPostCommentsByAuthor", method = RequestMethod.POST)
    private Map<String, Object> getPostCommentsByAuthor(Integer postcmt_author) {
        Map<String, Object> modelMap = new HashMap<>();
        List<PostCmt> postCmts = commentService.getPostCommentsByAuthor(postcmt_author);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", postCmts);
        return modelMap;
    }

    @RequestMapping(value = "addPostComment", method = RequestMethod.POST)
    private Map<String, Object> addPostComment(@RequestBody PostCmt postCmt) {
        Map<String, Object> modelMap = new HashMap<>();
        System.out.println("\n" + postCmt.getPostcmt_time() + "\n");
        if (commentService.addPostComment(postCmt)) {
            modelMap.put("code", "0");
            modelMap.put("info", "添加成功");
        } else {

            modelMap.put("code", "-1");
            modelMap.put("info", "添加失败");
        }
        return modelMap;
    }

//    @RequestMapping(value = "deletePostComment", method = RequestMethod.POST)
//    private Map<String, Object> deletePostComment(Integer postcmt_id) {
//        Map<String, Object> modelMap = new HashMap<>();
//        if (commentService.deletePostComment(postcmt_id)) {
//            modelMap.put("code", "0");
//            modelMap.put("info", "删除成功");
//        } else {
//
//            modelMap.put("code", "-1");
//            modelMap.put("info", "删除失败");
//        }
//        return modelMap;
//    }

    @RequestMapping(value = "getShopCommentsByShop", method = RequestMethod.POST)
    private Map<String, Object> getShopCommentsByShop(Integer shopcmt_shop) {
        Map<String, Object> modelMap = new HashMap<>();
        List<ShopCmt> shopCmts = commentService.getShopCommentsByShop(shopcmt_shop);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", shopCmts);
        return modelMap;
    }

    @RequestMapping(value = "getShopCommentsDetail", method = RequestMethod.POST)
    private Map<String, Object> getShopCommentsDetail(Integer shopcmt_id) {
        Map<String, Object> modelMap = new HashMap<>();
        ShopCmt shopCmt = commentService.getShopCommentsDetail(shopcmt_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", shopCmt);
        return modelMap;
    }

    @RequestMapping(value = "addShopComment", method = RequestMethod.POST)
    private Map<String, Object> addShopComment(@RequestBody ShopCmt shopcmt) {
        Map<String, Object> modelMap = new HashMap<>();

        if (commentService.addShopComment(shopcmt) && shopService.updateShopRating(shopcmt.getShopcmt_shop(), shopcmt.getShopcmt_rating(), 1)) {
            modelMap.put("code", "0");
            modelMap.put("info", "添加成功");
        } else {

            modelMap.put("code", "-1");
            modelMap.put("info", "添加失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "deletePostCommentInVue", method = RequestMethod.POST)
    private Map<String, Object> deletePostCommentInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (commentService.deletePostComment(Integer.parseInt(map.get("postcmt_id").toString()),
                Integer.parseInt(map.get("postcmt_post").toString()))) {
            modelMap.put("code", "0");
            modelMap.put("info", "删除成功");
        } else {

            modelMap.put("code", "-1");
            modelMap.put("info", "删除失败");
        }
        return modelMap;
    }


    @RequestMapping(value = "getPostCommentsInVue", method = RequestMethod.POST)
    private Map<String, Object> getPostCommentsInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        List<PostCmt> postCmts = commentService.getPostCommentsByPost(Integer.parseInt(map.get("postcmt_post").toString()));
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", postCmts);
        return modelMap;
    }

    @RequestMapping(value = "deleteShopCommentInVue", method = RequestMethod.POST)
    private Map<String, Object> deleteShopCommentInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (commentService.deleteShopComment(Integer.parseInt(map.get("shopcmt_id").toString()),
                Integer.parseInt(map.get("shopcmt_shop").toString()), Integer.parseInt(map.get("shopcmt_rating").toString()))) {
            modelMap.put("code", "0");
            modelMap.put("info", "删除成功");
        } else {

            modelMap.put("code", "-1");
            modelMap.put("info", "删除失败");
        }
        return modelMap;
    }


    @RequestMapping(value = "getShopCommentsInVue", method = RequestMethod.POST)
    private Map<String, Object> getShopCommentsInVue(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        List<ShopCmt> shopCmts = commentService.getShopCommentsByShop(Integer.parseInt(map.get("shopcmt_shop").toString()));
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", shopCmts);
        return modelMap;
    }


}

